package main

import (
	"context"
	"fmt"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"log"
	"time"
)

func main() {
	var (
		client *mongo.Client
		err error
		//database *mongo.Database
		//collection *mongo.Collection
	)
	// 1, 建立连接

	options := options.Client().ApplyURI("mongodb://127.0.0.1:27017").SetConnectTimeout(5 * time.Second)


	if client, err = mongo.Connect(context.TODO(), options); err != nil {
		log.Fatal(err)
		return
	}

	// Check the connection
	err = client.Ping(context.TODO(), nil)

	fmt.Println("Connected to MongoDB!")
	//
	//// 2, 选择数据库my_db
	//database = client.Database("my_db")
	//
	//// 3, 选择表my_collection
	//collection = database.Collection("my_collection")
	//
	//collection = collection
}
